package cn.item.util;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletPayResult
 */
@WebServlet("/ServletPayResult")
public class ServletPayResult extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletPayResult() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("-------servlet已经执行--------");
		
		try {
			String r0_Cmd = request.getParameter("r0_Cmd"); //业务类型
			String r1_Code = request.getParameter("r1_Code"); //扣款结果,该字段值为1时表示扣款成功.
			String r2_TrxId = request.getParameter("r2_TrxId"); //YeePay易宝交易订单号
			String r3_Amt = request.getParameter("r3_Amt");//扣款金额,交易结束后,YeePay易宝交易系统将实际扣款金额返回给商户
			String r4_Cur = request.getParameter("r4_Cur");//交易币种,人民币为CNY
			String r5_Pid = request.getParameter("r5_Pid");//商品ID
			String r6_Order = request.getParameter("r6_Order");//商户订单号
			String r7_Uid = request.getParameter("r7_Uid");//YeePay易宝会员ID
			String r8_MP  = request.getParameter("r8_MP");//商户扩展信息,可以任意填写1K 的字符串,交易返回时将原样返回
			String r9_BType = request.getParameter("r9_BType");//交易结果通知类型,1: 交易成功回调(浏览器重定向)2: 交易成功主动通知(服务器点对点通讯)
			String rb_BankId  = request.getParameter("rb_BankId");//支付银行
			String rp_PayDate = request.getParameter("rp_PayDate");//在银行支付时的时间p1_MerId
			String p1_MerId = request.getParameter("p1_MerId");//商户编号
			String hmac = request.getParameter("hmac");//hmcc

			System.out.println("商户编号："+p1_MerId);
			System.out.println("hmac:"+hmac);
			System.out.println("业务类型："+r0_Cmd);
			System.out.println("扣款结果："+r1_Code);
			System.out.println("扣款金额："+r3_Amt);
			System.out.println("交易结果通知类型："+r9_BType);
			System.out.println("支付银行："+rb_BankId);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		response.setHeader("Content-Type","text/event-stream");
		response.setHeader("Cache-Control", "no-cache");
		String data = "success";
		OutputStream out = response.getOutputStream();
		out.write(data.getBytes("utf-8"));

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
